package com.lagou.dao;

import com.lagou.entity.User;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDao {

    public int register(User user) throws SQLException {

        //1.创建QueryRunner
        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);
        //2.编写SQL
        String sql  = "insert into user values(?,?,?,?,?,?)";

        Object[] pram = {user.getUid(),user.getUsername(),user.getPassword(),user.getTelephone(),user.getBirthday(),user.getSex()};

        int update = qr.update(sql, pram);

        return update;
    }
    public User login(String username,String password) throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);

        String sql = "select * from user where username = ? and password =?";

        User user = qr.query(sql, new BeanHandler<User>(User.class), username, password);
        return user;
    }
}
